/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

/**
 * Prototype for a player w/scrubber and timecode display
 * @TODO move into an object
 * @TODO Separate display of the scrubber from actual timecode
 *  and implement events for update of the timecode so the scrubber
 *  won't be "jumpy"
 *  @TODO Check this with lodewijk ;-)
 */
/*
$().ready(function( evt ){
    var player      = $('.currentVideo')[0];
    var playPause   = $('.playPause'); 
    var progressBar = $('.progressBar'); 
    var timeCode    = $('.timeCode');
    
    function updatePlayerControls(){
        time = getCurrentTime();
        updateTimeCode( time );
        updateProgressBar( time );
    }
    
    function updateProgressBar( time ){
        var duration = getDuration();
        
        if( duration > 0 ){
            var percent = Math.floor( time / duration * 100 );
        }

        $('.progressBarProgress').css('width', percent + '%' );
    }
    
    function updateTimeCode( time ){
        timeCode.html( makeTimeCode( time ) );
    }
    
    function makeTimeCode( time ){
        var s = parseInt(time % 60);
        var m = parseInt( time / 60 );
        var h = parseInt( time / 3600 );
        
        var timecode = numberPad(h,1)
                    + ':' + numberPad(m,1)
                    + ':' + numberPad(s,1)
        ;
        return timecode;
    }

    function numberPad( num, padding ){
        var s = parseInt(num).toString();
        
        while( s.length < padding+1 ){
            s = '0' + s;
        }
        
        var decimal = num.toString().split('.')[1];
        if( decimal !== undefined ){
            s = s + '.' + decimal;
        }
        
        return s;
    }
    
    function getCurrentTime(){
        return player.currentTime;
    }
    
    function getDuration(){
        if( player.duration === NaN || player.duration == 'Inf' ){
            return 0;
        }
        return player.duration;
    }
    
    function startTimer(){
        player.timer = setInterval(function(){
            updatePlayerControls();
        }, 500 );
    }
    
    function stopTimer(){
        clearInterval( player.timer );
    }

    function scrub( posX ){
        var dim = getProgressBarDimensions();
        var part    = posX - dim.left;        
        var percent = Math.round( part / dim.w * 100 );
        
        if( percent > 99 ){
            percent = 99;
        }
        
        var time = Math.round(getDuration() * ( percent/100 ));
        seek( time );
    }
    
    function getProgressBarDimensions(){
        if( this.progressBarDimensions === undefined ){
            var dim = {w: progressBar.width(), h:progressBar.height()};
            var pos = progressBar.offset();
            
            dim.left = pos.left;
            dim.top  = pos.top;
            this.progressBarDimensions = dim;
        }
        return this.progressBarDimensions;
    }
    
    function getPlaybackStatus(){
        if( this.playbackStatus === undefined ){
            this.playbackStatus = 'paused';
        }
        return this.playbackStatus;
    }
    
    function setPlaybackStatus( value ){
        this.playbackStatus = value;
    }
    
    function setCurrentTime( time ){
        player.currentTime = time;
    }
    
    function seek( time ){
        player.currentTime = time;        
        status = getPlaybackStatus();
        
        if( status == 'playing' ){
            pause();
        }
        
        if( getPlaybackStatus() != status ){
            play();
        }

        updateTimeCode( time );
        updateProgressBar( time );
    }
    
    function pause(){
        setPlaybackStatus('paused');
        player.pause();
    }
    
    function play(){
        setPlaybackStatus('playing');
        player.play();
    }
    
    $('#mainPlayer').bind('mouseover mouseout', function( evt ){
        if( evt.type === 'mouseout' ){
            $('.playerControls').hide();
        }
        else{
            $('.playerControls').show();
        }
    });
    
    $('.progressBar').bind('mousedown mouseup mousemove', function( evt ){
        if( evt.type == 'mousedown' ){
            this.canScrub = true;
        }
        else if( evt.type == 'mouseup' ){
            this.canScrub = false;
        }
        
        if( this.canScrub ){ 
            scrub( evt.clientX );
        }    
    });
    
        $('.playPause').bind('click', function( evt ){
        if( getPlaybackStatus() == 'playing' ){
            pause();
        }
        else{
            play();
        }
    });

    $(player).bind('play pause', function( evt ){
        if( evt.type === 'pause' ){
            $('.playPause')[0].innerHTML = 'play';
            setPlaybackStatus('paused');
            stopTimer();
        }
        else{
            $('.playPause')[0].innerHTML = 'pause';
            setPlaybackStatus('playing');
            startTimer();
        }        
    });
});
 */